from blocks import *
import fileinput

def processline(line):
    line=line[:-1]
    while len(line)>0 and line[-1]==" ":
        line=line[:-1]
    indent=0
    for i in range(len(line)):
        if line[i]!=" ":
            break
        indent=indent+1
    return (indent,line)
        
def main():
    filename=raw_input("filename: ")
    f=open(filename,'r')
    f.readline()
    title=processline(f.readline())[1]
    date=processline(f.readline())[1]
    presenter=processline(f.readline())[1]
    affiliation=processline(f.readline())[1]

    path=[]
    indentrecord=[]
    current=None
    currentindent=0
    flag=0
    for line in fileinput.input(filename):
        print line
        if line=="slides:\n":
            flag=1
        if flag==0:
            continue
        tmp=processline(line)
        indent=tmp[0]
        line=tmp[1]
        print currentindent
        print indent
        if len(line)==0:
            continue
        if(currentindent==-1):
            if(indent<=currentindent):
                print "incorrect indentation at",fileinput.lineno()
                return
            indentrecord.append(indent)
            path.append(current)            
            currentindent=indent
            print currentindent

        elif(indent<currentindent):
            print indentrecord
            while(indent<indentrecord[-1]):  
                print "pop"
                indentrecord.pop()
                path.pop()
                
                current=path[-1]
                currentindent=indentrecord[-1]

                print path
                print current.brief()
                print currentindent
                print indent

        elif(indent>currentindent):
            print "incorrect indentation at",fileinput.lineno()
            return
        else:
            pass
          
        if(current==None):
            if line=="slides:" :
                presentation=Presentation(title,date,presenter,affiliation)
                current=presentation
                currentindent=-1
        elif(current==presentation):
            if(len(line)==0 or line[-1]!=":"):
                print "syntax error at", fileinput.lineno()
                return
            tmp=Slide()
            current.addSlide(tmp)              
            current=tmp
            current.addTitle(line[:-1])
            tmp=Block("ul")
            current.addContent(tmp)
            print "append"
            current=tmp
            currentindent=-1
        elif(line[-1]==":"):
            current.addContent(Block("li",Text(line[:-1])))
            tmp=Block("ul")
            current.addContent(tmp)
            current=tmp
            currentindent=-1
        else:
            current.addContent(Block("li",Text(line)))
    f=open(filename+".html",'w')
    presentation.output(f)      
    return        

                
if __name__=="__main__":
    main()